<?php

Namespace Cyfy\Modules\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms\Module as Module;

/**
 * Wysiwyg editor, uses TinyMCE.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class WysiwygEditor extends Input
{
    
    /**
     * Creates a wysiwyg element.
     *
     * @param string Textbox name
     * @param string Title
     * @param string Default value
     * @param bool Is required
     */
    function __construct($name, $title, $value = '', $required = false)
    {
        $this -> name = $name;
        $this -> title = $title;
        $this -> value = $value;
        $this -> required = $required;
    }

    /**
     * Returns a string containing the wysiwyg textarea.
     * @return string HTML content
     */
     function __toString()
    {
        $required = ($this->required) ? " form-validate" : "";
        $output = "<div class=\"form-item form-item-{$this->name}{$required}\"> \n";
        if(\Cyfy\Modules :: isEnabled('TinyMCE'))
        {
            $output .= \Cyfy\Modules\TinyMCE\TinyMCE :: get($this->title, $this->name, $this->value);
        }
        else
        {
            $output .= \Cyfy\Message :: get(Module :: t(210), \Cyfy\Message::ERROR);
        }
        $output .= "</div> \n\n";
        return $output;
    }
    
    /**
     * Loads data from post to the object.
     * Fixes some security stuff.
     *
     * @return array array(name => value)
     */
    function loadPostData($prefix)
    {
        // load
        $this -> value = $_POST[$prefix . $this -> name];
        // security
        return array($this -> name => $this -> value);
    }
    
    function validate()
    {
        // required
        if($this->required){
            if($this -> value == ""){
                Message :: set(Module :: t(221, array($this -> title)), Message::ERROR);
                return false;
            }
        }
        return true;
    }
}